package com.heima.search.service.impl;

import com.heima.common.constant.GlobalConstant;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.dtos.HistorySearchDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.search.pojos.ApUserSearch;
import com.heima.search.service.ApUserSearchService;
import com.heima.utils.threadLocal.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ApUserSearchServiceImpl implements ApUserSearchService {
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    @Async
    public void insert(String keyword, Integer userId) {

        ApUserSearch apUserSearch = mongoTemplate.findOne(
                Query.query(Criteria.where(GlobalConstant.USER_ID_TAG).is(userId)
                        .and(GlobalConstant.KEYWORD_TAG).is(keyword))
                , ApUserSearch.class);
        if (apUserSearch!=null)
        {
            apUserSearch.setCreatedTime(new Date());
            mongoTemplate.save(apUserSearch);
            return;
        }
        List<ApUserSearch> apUserSearches = mongoTemplate.find(
                Query.query(Criteria.where(GlobalConstant.USER_ID_TAG).is(userId))
                        .with(Sort.by(Sort.Direction.DESC,GlobalConstant.CREATED_TIME_TAG))
                , ApUserSearch.class);
        apUserSearch =new ApUserSearch();
        apUserSearch.setUserId(userId);
        apUserSearch.setKeyword(keyword);
        apUserSearch.setCreatedTime(new Date());
        if (apUserSearches.size()>=GlobalConstant.SEARCH_RECORD_MAX)
        {
            ApUserSearch del = apUserSearches.get(apUserSearches.size() - 1);
            mongoTemplate.findAndReplace(Query.query(Criteria.where(GlobalConstant.ID_TAG).is(del.getId())),apUserSearch);

        }
        else mongoTemplate.save(apUserSearch);
    }

    @Override
    public ResponseResult findUserSearch() {
        List<ApUserSearch> apUserSearches = mongoTemplate.find(
                Query.query(Criteria.where(GlobalConstant.USER_ID_TAG).is(AppThreadLocalUtil.getUserId()))
                        .with(Sort.by(Sort.Direction.DESC,GlobalConstant.CREATED_TIME_TAG))
                , ApUserSearch.class);
        return ResponseResult.okResult(apUserSearches);
    }

    @Override
    public ResponseResult delUserSearch(HistorySearchDto dto) {
        if (dto==null||dto.getId()==null)
        {
            throw new CustomException(AppHttpCodeEnum.PARAMS_NOT_NULL);
        }
        mongoTemplate.remove(
                Query.query(
                        Criteria.where(GlobalConstant.USER_ID_TAG).is(AppThreadLocalUtil.getUserId())
                                .and(GlobalConstant.ID_TAG).is(dto.getId()))
                ,ApUserSearch.class);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
